package com.bookShop.action;

import java.util.HashMap;
import java.util.Map;


import com.bookShop.entity.CartItem;
import com.bookShop.entity.Book;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;


public class CartAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	private CartItem cartItem;
	private Book book;
	
	@SuppressWarnings("unchecked")
	public String getCart() throws Exception{
		

		Map cart = (Map)ActionContext.getContext().getSession().get("CART");
		if(cart==null){
			cart=new HashMap();
		}
		book = (Book) ActionContext.getContext().getSession().get("OB");
		cartItem = (CartItem)cart.get(book.getId());
		if(cartItem==null){
			cartItem = new CartItem(book,1);
		}else{
			cartItem.setNumber(cartItem.getNumber()+1);
		}
		cart.put(book.getId(), cartItem);
		ActionContext.getContext().getSession().put("CART", cart);
		return SUCCESS;
	}
}
